package com.atguigu.chapter07.state;

import org.apache.flink.api.common.state.BroadcastState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ReadOnlyBroadcastState;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.BroadcastStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/7/20 11:40
 */
public class Flink03_State_Operate_BroadCast {
    
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        // 两个流: 一个数据流, 正常的数据. 另外一个流是: 控制流, 用来动态控制数据流中的数据的处理逻辑
        
        DataStreamSource<String> dataStream = env.socketTextStream("hadoop162", 8888);
        DataStreamSource<String> controlStream = env.socketTextStream("hadoop162", 9999);
        
        // 1. 把控制流做成广播流
        MapStateDescriptor<String, String> bdStateDesc = new MapStateDescriptor<>("bdState", String.class, String.class);
        BroadcastStream<String> bdStream = controlStream
            .broadcast(bdStateDesc);
        
        // 2. 用数据流去connect广播流
        dataStream
            .connect(bdStream)
            .process(new BroadcastProcessFunction<String, String, String>() {
                // 处理数据流的数据
                @Override
                public void processElement(String value,
                                           ReadOnlyContext ctx,
                                           Collector<String> out) throws Exception {
                    ReadOnlyBroadcastState<String, String> bdState = ctx.getBroadcastState(bdStateDesc);
                    String v = bdState.get("switch");
                    if ("1".equals(v)) {
                        out.collect("使用1号逻辑处理数据");
                    } else if ("2".equals(v)) {
                        out.collect("使用2号逻辑处理数据");
                    } else {
                        out.collect("使用默认逻辑处理数据");
                    }
                }
                
                // 处理广播流的数据
                @Override
                public void processBroadcastElement(String value,
                                                    Context ctx,
                                                    Collector<String> out) throws Exception {
                    // 把每天数据放入到广播状态
                    BroadcastState<String, String> bdState = ctx.getBroadcastState(bdStateDesc);
                    bdState.put("switch", value);
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}
/*
广播状态:
    广播状态的本质其实是一个Map集合
    
    先有一个广播流, 广播流再和普通的数据流进行connect


 */
